home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / des3.zip / DES.C < prev    next >
C/C++ Source or Header  |  1995-04-01  |  3KB  |  96 lines

  1. /*
  2.  * this sample source will give you an idea on how to
  3.  * call up the various library functions. -SW
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <fcntl.h>
  9. #include <time.h>
  10. #include "des3.h"
  11.  
  12. #define BUFSIZE 16384
  13.  
  14. static char Usage[] = "des [-]{d|e}[3] infile outfile key";
  15.  
  16. int main(int argc, char **argv)
  17. {
  18.   register uchar *b, *buf, c;
  19.   static uchar key[8], key2[16];
  20.   register int i, what, mode, bytes;
  21.   static int last = 0;
  22.   register time_t t, t1;
  23.   FILE *ih, *oh;
  24.   if(argc == 5) {
  25.     switch (c = *argv[1] == '-' ? *++argv[1] : *argv[1]) {
  26.       case 'e': case 'E': what = 0; break;
  27.       case 'd': case 'D': what = 1; break;
  28.       default : fprintf(stderr,"des : illegal option %c \n",c);
  29.               fprintf(stderr,"Usage : %s \n",Usage);
  30.               return 1;
  31.     }
  32.     switch(*++argv[1]) {
  33.       case '3': mode = 3; break;
  34.       default : mode = 1;
  35.     }
  36.   } else {
  37.     fprintf(stderr,"Usage : %s \n",Usage);
  38.     return 1;
  39.   }
  40.   if ((buf = (uchar *) malloc(BUFSIZE)) == NULL) {
  41.     fprintf(stderr,"Error: Cannot alloc memory !\n");
  42.     return 1;
  43.   }
  44.   ih =  fopen(argv[2],"rb");
  45.   oh = fopen(argv[3],"wb");
  46.   if (ih == NULL || oh == NULL) {
  47.     perror(ih ? argv[3]:argv[2]);
  48.     return 1;
  49.   }
  50.   if (mode == 1) {
  51.     memset(key,0,8);
  52.     bytes = strlen(argv[4]);
  53.     memcpy(key,argv[4],bytes > 7 ? 8:bytes);
  54.     desinit(key);
  55.   } else {
  56.     memset(key2,0,16);
  57.     bytes = strlen(argv[4]);
  58.     memcpy(key2,argv[4],bytes > 15 ? 16:bytes);
  59.     des3init(key2);
  60.   }
  61.   t = time(NULL);
  62.   while ((i = fread(buf,1,BUFSIZE,ih)) != 0) {
  63.     for (b=buf, bytes=i; i > 0; i -= 8 , b += 8) {
  64.       if (!what) {
  65.         if (i < 8) {
  66.           bytes += 8-i;
  67.           last = i;
  68.         }
  69.         if (mode == 1) ecbencode(b,b); else ecb3encode(b,b);
  70.       } else {
  71.         if (i > 7) {
  72.           if (mode == 1) ecbdecode(b,b); else ecb3decode(b,b);
  73.         } else bytes -= (9 - (b[0] & 0xff));
  74.       }
  75.     }
  76.     if (!what) {
  77.       if (fwrite(buf,1,bytes,oh) != bytes) {
  78.         fprintf(stderr,"Error in write !\n");
  79.         return 1;
  80.       }
  81.     } else {
  82.       if (fwrite(buf,1,bytes,oh) != bytes) {
  83.         fprintf(stderr,"Error in write !\n");
  84.         return 1;
  85.       }
  86.     }
  87.   }
  88.   if (!what) fputc(last ? last : last + 8,oh);
  89.   t1 = time(NULL) - t;
  90.   printf("%scrypted bytes per second using %s DES: %ld\n", what ? "De":"En",
  91.           mode == 1 ? "single" : "triple" , ftell(ih)/(t1 ? t1 : 1));
  92.   fclose(ih);
  93.   fclose(oh);
  94.   return 0;
  95. }
  96.